package com.test.smartcampusapi.controller;

import com.test.smartcampusapi.entity.User;
import com.test.smartcampusapi.service.CardService;
import com.test.smartcampusapi.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * Date : 4/22/2022 10:06 PM
 * Author : lingy
 * Description : ...
 */
@RestController
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private CardService cardService;

    @PostMapping(value = "/user/getAll")
    public List<User> getAllUsers() {
        return userService.getAllUsers();
    }

    @PostMapping(value = "/user/addUser")
    public int addUser(@RequestBody Map<String, Object> user) {
        try {
            // 注册成功
            userService.addUser(user.get("account").toString(), user.get("password").toString());
            return 0;
        } catch (Exception e) {
            return 1;

        }
    }

    @PostMapping(value = "/user/login")
    public int login(@RequestBody Map<String, Object> user) {
        try {
            if (userService.login(user.get("account").toString(), user.get("password").toString()) == 1) {
                return 0;
            } else {
                return 1;
            }
        } catch (Exception e) {
            return 1;
        }
    }

    @PostMapping(value = "/user/updateInfo")
    public int createBanner(@RequestBody Map<String, String> userInfo) {
        try {
            if (userService.updateInformation(
                    userInfo.get("userAccount"),
                    userInfo.get("userGender"),
                    userInfo.get("userDescription"),
                    userInfo.get("userPhone")
            ) == 1) {
                return 0;
            } else {
                return 1;
            }

        } catch (Exception e) {
            return 1;
        }
    }

    @PostMapping(value = "/user/bindCard")
    public int bindCard(@RequestBody Map<String, Object> binding) {
        try {
            String userAccount = binding.get("userAccount").toString();
            String cardNumber = binding.get("cardNumber").toString();
            //绑定时，必须传卡号和用户名
            userService.bindCard(userAccount, cardNumber);

            //设置卡状态为绑定binding
            cardService.updateCardStatus(cardNumber, "binding");

            return 0;
        } catch (Exception e) {
            return 1;
        }
    }


    @PostMapping(value = "/user/unBindCard")
    public int unBindCard(@RequestBody Map<String, Object> binding) {
        try {
            String userAccount = binding.get("userAccount").toString();
            //绑定时，必须传卡号和用户名
            userService.unBindCard(userAccount);
            //设置卡状态为绑定available
            //cardService.updateCardStatus(cardNumber,"binding");

            return 0;
        } catch (Exception e) {
            return 1;
        }
    }
    @PostMapping(value = "/user/userInfo")
    public  User getUserInfo(@RequestBody Map<String,String> userAccount){
        System.out.println("called");
        try{
            User user = userService.getUserInfo(userAccount.get("userAccount"));
            return  user;
        } catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }
}
